package com.javaenzk.wfx.manager.service;

import com.javaenzk.wfx.manager.dao.UserDAO;
import com.javaenzk.wfx.manager.po.SysModule;
import com.javaenzk.wfx.manager.po.User_Info;
import com.javaenzk.wfx.manager.vo.ParentNode;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.Unsafe;
import sun.security.provider.MD5;

import javax.annotation.Resource;
import java.net.PasswordAuthentication;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserService {

    @Resource
    private UserDAO userDAO;

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    public User_Info login(String username,String password) {

        if (username == null || username == ""){
            throw new  NullPointerException("username is null");
        }

        if (password == null || password == ""){
            throw new NullPointerException("password is null");
        }

       User_Info userInfo= userDAO.findByUserName(username);

        if (userInfo == null){
            throw  new  AuthenticationException();  //没有这个用户
        }

        //获取加盐的秘密
        //1.获取盐
        String soal = userInfo.getAccount()+userInfo.getUserId();

        //2.加密
        Md5Hash md5Hash = new Md5Hash(password,soal);
        String md5Hex = md5Hash.toHex();  //编码成16位的密码


        //判断加密之后的密码是否与数据库的秘密一致
        if(!md5Hex.equals(userInfo.getPassword())){
            throw  new IncorrectCredentialsException(); //shiro验证密码不正确的异常
        }

        return userInfo;
    }


    /**
     * 查询所有的权限信息
     * @return
     * @throws Exception
     */
    public List<SysModule> queryAllModule() throws  Exception{
        return userDAO.queryAllModuld();
    }

    /**
     * 查询角色拥有的权限
     * @return
     * @throws Exception
     */
    public  List<SysModule> queryHasModule(String account) throws Exception{
        return userDAO.queryHasModuld(account);
    }

    /**
     * 单独查询所有权限的名字用来过滤
     * @return
     */
    public List<String> queryAllModuleName(String account) throws  Exception{
        return userDAO.queryAllModuleName(account);
    }


    /**
     * 查询出用户没有的权限
     * @return
     */
    public List<SysModule> queryHasNoModule(String account) throws  Exception{

        //查询出用户拥有的权限的名字
        final List<String> stringList = queryAllModuleName(account);
        //查询出数据库所有的权限
        final List<SysModule> sysModulesall = queryAllModule();
        //准备一个集合，用来接收用户没有的权限
        List<SysModule> nohasModelulist = new ArrayList<SysModule>();
        for (int i=0;i<sysModulesall.size();i++){
            //如果权限名字不在用户的权限名字之内，证明没有这个权限
            if (!stringList.contains(sysModulesall.get(i).getModule_Name())){
                nohasModelulist.add(sysModulesall.get(i));
            }
        }
            return nohasModelulist;
    }

    /**
     * 查询出所有的节点信息
     * @return
     * @throws Exception
     */
    public List<ParentNode> queryParent(String roleCode) throws  Exception{
        return userDAO.queryParentNodes(roleCode);
    }

    /**
     * 为用户添加权限
     * @param moduleId
     * @param roleId
     */
    public void  addSysmodule(String[] moduleId,String roleId) throws Exception{

        if (moduleId.length == 0 || moduleId == null)
        {
            throw  new  NullPointerException("moduleId is null");
        }

        userDAO.addSysmodule(moduleId,roleId);
    }

    /**
     * 为用户删除权限
     * @param moduleId
     * @param roleId
     */
    public  void  delSysmodule(String[] moduleId,String roleId) throws  Exception{

        if (moduleId.length == 0 || moduleId == null)
        {
            throw  new  NullPointerException("moduleId is null");
        }

        userDAO.delSysmodule(moduleId,roleId);



    }

     public List<String> queryFuncByUseryy(String account){
        return userDAO.queryFuncByUser(account);
    }

}
